<template>
	<view class="goods_detail">
		<view>
			<swiper indicator-dots>
				<swiper-item>
					<image :src="info.img"></image>
				</swiper-item>
			</swiper>
			<view class="box1">
				<view class="price">
					<text>￥{{info.price}}</text>
					<text>￥{{info.price}}</text>
				</view>
				<view class="goods_name">{{info.name}}</view>
			</view>
		</view>
		<view class="line"></view>
		<view class="box2">
			<!-- <view>货号：{{info.goods_no}}</view> -->
			<view>库存：{{info.store}}</view>
		</view>
		<view class="line"></view>
		<view class="box3">
			<view class="tit">详情介绍</view>
			<!-- 			<view class="content">
				<rich-text :nodes="content"></rich-text>
			</view> -->
		</view>
		<view class="goods_nav">
			<uni-goods-nav :fill="true" :options="options" :button-group="buttonGroup" @click="onClick"
				@buttonClick="buttonClick"></uni-goods-nav>
		</view>
	</view>
</template>

<script>
	import {
		serverIp
	} from "../../common/config.js";
	import uniGoodsNav from '@/components/uni-goods-nav/uni-goods-nav.vue'
	export default {
		data() {
			return {
				id: 0,
				user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
				swipers: [],
				info: [],
				no: null,
				serverIp: serverIp,
				form: {
					num: 1
				},
				// content: '',
				options: [{
					icon: 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/uni-ui/goodsnav/kefu.png',
					text: '客服'
				}, {
					icon: 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/uni-ui/goodsnav/dianpu.png',
					text: '店铺'
				}, {
					icon: 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/uni-ui/goodsnav/carts.png',
					text: '购物车',
					info: 12
				}, ],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			}
		},
		onLoad(options) {
			this.id = options.id
			// this.getSwipers()
			this.getDetailInfo()
			// this.getDetailContent()
			const currentDate = new Date();
			const formattedDate = currentDate.getFullYear() + (currentDate.getMonth() + 1).toString().padStart(2, '0') +
				currentDate.getDate().toString().padStart(2, '0');
			const currentTimeMillis = currentDate.getTime();
			this.no = formattedDate + currentTimeMillis
			console.log(formattedDate + currentTimeMillis);
		},
		components: {
			uniGoodsNav
		},

		methods: {

			// async getSwipers() {
			// 	// const res = await this.$myRuquest({
			// 	// 	url: '/api/getthumimages/'+this.id
			// 	// })
			// 	// this.swipers = res.data.message


			// 	this.request.get("/goods/" + this.id).then((res) => {
			// 		if (res.code === "200") {
			// 			console.log("商品详情", res)
			// 			this.swipers = res.data
			// 		}
			// 	})

			// },
			async getDetailInfo() {
				// const res = await this.$myRuquest({
				// 	url: '/api/goods/getinfo/'+this.id
				// })
				// this.info = res.data.message[0]

				this.request.get("/goods/" + this.id).then((res) => {
					if (res.code === "200") {
						this.info = res.data
						this.info.num = 1
						console.log("商品详情2", res)
					}
				})

			},
			// async getDetailContent () {
			// 	const res = await this.$myRuquest({
			// 		url: '/api/goods/getdesc/'+this.id
			// 	})
			// 	this.content = res.data.message[0].content
			// },
			onClick(e) {
				console.log(e)

				uni.showToast({
					title: `点击${e.content.text}`,
					icon: 'none'
				})
			},
			buttonClick(e) {
				console.log(e)
				if (e.index == 1) {
					// let data = {
					// 	name: this.info.name,
					// 	totalPrice: this.info.price,
					// 	carts: this.info
					// }
					// console.log(data)
					// this.request.post("/orders", data).then(res => {
					// 	if (res.code === "200") {
					// 		this.$message.success("正在前往支付");
					// 	} else {
					// 		this.$message.error(res.msg);
					// 	}
					// })
					window.open(`${this.serverIp}/alipay/pay?subject=${this.info.name}&traceNo=${this.no}&totalAmount=${this.info.price}`);
					// uni.showToast({
					// 	title: '未开发,请点击加入购物车',
					// 	icon: 'error',
					// 	//显示持续时间为 2秒
					// 	duration: 2000,
					// })

				} else {
					if (!this.user.username) {
						this.$message.warning("请登录后操作")
						return
					}
					// 商品 id
					this.form.goodsId = this.info.id
					console.log('this.form', this.form)

					this.request.post("/cart", this.form).then(res => {
						if (res.code === "200") {
							this.$message.success("加入购物车成功")
						} else {
							this.$message.error(res.msg)
						}
					})
				}
				this.options[2].info++
			}
		},

	}
</script>

<style lang="scss">
	.goods_detail {
		swiper {
			height: 700rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.box1 {
			padding: 10px;

			.price {
				font-size: 35rpx;
				color: $shop-color;
				line-height: 80rpx;

				text:nth-child(2) {
					color: #ccc;
					font-size: 28rpx;
					text-decoration: line-through;
					margin-left: 20rpx;
				}
			}

			.goods_name {
				font-size: 32rpx;
				line-height: 60rpx;
			}
		}

		.box2 {
			padding: 0 10px;
			font-size: 32rpx;
			line-height: 70rpx;
		}

		.box3 {
			padding-bottom: 50px;

			.tit {
				font-size: 32rpx;
				padding-left: 10px;
				border-bottom: 1px solid #eee;
				line-height: 70rpx;
			}

			.content {
				padding: 10px;
				font-size: 28rpx;
				color: #333;
				line-height: 50rpx;
			}
		}
	}

	.goods_nav {
		position: fixed;
		bottom: 0;
		width: 100%;
	}

	.line {
		height: 10rpx;
		width: 750rpx;
		background: #eee;
	}
</style>
